package com.mofang.powerdekorhelper.persenter;

import com.google.gson.Gson;
import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ResultMessage2;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.ChangeAccountView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/17 0017.
 */

public class ChangeAccountPresenter extends BasePresent<ChangeAccountView> {

    RetrofitSerives retrofitSerives;
    Call<ResultMessage2> call;

    public void getChangeAccountResult(int id, String account) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", id);
            jsonObject.put("account", account);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Gson gson = new Gson();
        String jsonParams = gson.toJson(jsonObject);

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddress);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        call = retrofitSerives.getChangeAccount(body);

        call.enqueue(new Callback<ResultMessage2>() {
            @Override
            public void onResponse(Call<ResultMessage2> call, Response<ResultMessage2> response) {

                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0") && response.body().getMessage().equals("成功")) {
                        view.setResult(response.body());
                        view.toast("修改账号成功");
                    }else {
                        view.toast(response.body().getMessage());
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage2> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });

    }

    @Override
    public void stopRequest() {

    }
}
